from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QColor, QFont


class MarkdownHighlighter(QSyntaxHighlighter):
    """Markdown语法高亮器"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.highlighting_rules = []
        self.setup_rules()
    
    def setup_rules(self):
        """设置Markdown语法高亮规则"""
        
        # 标题格式 - 深蓝色，加粗
        heading_format = QTextCharFormat()
        heading_format.setForeground(QColor('#569CD6'))  # 深蓝色
        heading_format.setFontWeight(QFont.Bold)
        
        # 1-6级标题
        heading_patterns = [
            QRegExp(r'^#{1,6}\s+.*'),  # # 标题
        ]
        for pattern in heading_patterns:
            self.highlighting_rules.append((pattern, heading_format))
        
        # 粗体格式 - 橙色，加粗
        bold_format = QTextCharFormat()
        bold_format.setForeground(QColor('#CE713A'))  # 橙色
        bold_format.setFontWeight(QFont.Bold)
        
        bold_patterns = [
            QRegExp(r'\*\*[^*]+\*\*'),  # **粗体**
            QRegExp(r'__[^_]+__'),       # __粗体__
        ]
        for pattern in bold_patterns:
            self.highlighting_rules.append((pattern, bold_format))
        
        # 斜体格式 - 紫色
        italic_format = QTextCharFormat()
        italic_format.setForeground(QColor('#B5CEA8'))  # 紫色
        italic_format.setFontItalic(True)
        
        italic_patterns = [
            QRegExp(r'\*[^*]+\*'),      # *斜体*
            QRegExp(r'_[^_]+_'),         # _斜体_
        ]
        for pattern in italic_patterns:
            self.highlighting_rules.append((pattern, italic_format))
        
        # 删除线格式 - 灰色
        strikethrough_format = QTextCharFormat()
        strikethrough_format.setForeground(QColor('#808080'))  # 灰色
        strikethrough_format.setFontStrikeOut(True)
        
        strikethrough_pattern = QRegExp(r'~~[^~]+~~')  # ~~删除线~~
        self.highlighting_rules.append((strikethrough_pattern, strikethrough_format))
        
        # 代码格式 - 绿色背景
        code_format = QTextCharFormat()
        code_format.setForeground(QColor('#6A8759'))  # 绿色
        code_format.setBackground(QColor('#2B2B2B'))  # 深灰色背景
        
        code_patterns = [
            QRegExp(r'`[^`]+`'),        # 行内代码
            QRegExp(r'^```[^`]*```$'),   # 代码块
        ]
        for pattern in code_patterns:
            self.highlighting_rules.append((pattern, code_format))
        
        # 链接格式 - 蓝色
        link_format = QTextCharFormat()
        link_format.setForeground(QColor('#4E9AEF'))  # 蓝色
        link_format.setFontUnderline(True)
        
        link_patterns = [
            QRegExp(r'\[[^\]]+\]\([^)]+\)'),  # [链接](url)
            QRegExp(r'https?://[^\s]+'),        # 直接URL
        ]
        for pattern in link_patterns:
            self.highlighting_rules.append((pattern, link_format))
        
        # 列表格式 - 青色
        list_format = QTextCharFormat()
        list_format.setForeground(QColor('#4EC9B0'))  # 青色
        
        list_patterns = [
            QRegExp(r'^\s*[-*+]\s+'),           # 无序列表
            QRegExp(r'^\s*\d+\.\s+'),          # 有序列表
        ]
        for pattern in list_patterns:
            self.highlighting_rules.append((pattern, list_format))
        
        # 引用格式 - 浅黄色
        blockquote_format = QTextCharFormat()
        blockquote_format.setForeground(QColor('#DCDCAA'))  # 浅黄色
        
        blockquote_pattern = QRegExp(r'^>\s+.*')  # > 引用
        self.highlighting_rules.append((blockquote_pattern, blockquote_format))
        
        # 水平线格式 - 灰色
        hr_format = QTextCharFormat()
        hr_format.setForeground(QColor('#808080'))  # 灰色
        
        hr_patterns = [
            QRegExp(r'^\s*---\s*$'),           # ---
            QRegExp(r'^\s*\*\*\*\s*$'),       # ***
            QRegExp(r'^\s*___\s*$'),           # ___
        ]
        for pattern in hr_patterns:
            self.highlighting_rules.append((pattern, hr_format))
        
        # 表格格式 - 粉色
        table_format = QTextCharFormat()
        table_format.setForeground(QColor('#CE9178'))  # 粉色
        
        table_pattern = QRegExp(r'^\|.*\|$')  # | 表格 |
        self.highlighting_rules.append((table_pattern, table_format))
    
    def highlightBlock(self, text):
        """高亮文本块"""
        for pattern, format_ in self.highlighting_rules:
            expression = QRegExp(pattern)
            index = expression.indexIn(text)
            while index >= 0:
                length = expression.matchedLength()
                self.setFormat(index, length, format_)
                index = expression.indexIn(text, index + length)